package com.research.controller;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.common.utils.CurrentRoleUtils;
import com.common.utils.SecurityUtils;
import com.common.utils.StringUtils;
import com.research.domain.entity.ResearchUnit;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.common.annotation.Log;
import com.common.core.controller.BaseController;
import com.common.core.domain.AjaxResult;
import com.common.enums.BusinessType;
import com.research.domain.entity.EducationStat;
import com.research.service.IEducationStatService;
import com.common.utils.poi.ExcelUtil;
import com.common.core.page.TableDataInfo;

/**
 * 研究机构-教育部统计Controller
 *
 * @author hst
 * @date 2025-03-17
 */
@RestController
@RequestMapping("/research/educationStat")
public class EducationStatController extends BaseController
{
    @Autowired
    private IEducationStatService educationStatService;

    /**
     * 查询研究机构-教育部统计列表
     */
    @PreAuthorize("@ss.hasPermi('research:educationStat:list')")
    @GetMapping("/list")
    public TableDataInfo list(EducationStat educationStat)
    {
        String currentRoleKey = CurrentRoleUtils.getCurrentRoleKey();
        if ("biz_kyms".equals(currentRoleKey) ) {
            Map<String, Object> map = educationStat.getParams();
            map.put("unitAdminScopeReal", SecurityUtils.getLoginUser().getUser().getUnitAdminScopeReal());
        }
        startPage();
        List<EducationStat> list = educationStatService.selectEducationStatList(educationStat);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('research:educationStat:list')")
    @GetMapping("/collect")
    public TableDataInfo collect(ResearchUnit educationStat)
    {
        String currentRoleKey = CurrentRoleUtils.getCurrentRoleKey();
        if ("biz_kyms".equals(currentRoleKey) ) {
            Map<String, Object> map = educationStat.getParams();
            map.put("unitAdminScopeReal", SecurityUtils.getLoginUser().getUser().getUnitAdminScopeReal());
        }
        startPage();
        List<ResearchUnit> list = educationStatService.selectEducationStatCollect(educationStat);
        return getDataTable(list);
    }

    /**
     * 导出研究机构-教育部统计列表
     */
    @PreAuthorize("@ss.hasPermi('research:educationStat:export')")
    @Log(title = "研究机构-教育部统计", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EducationStat educationStat)
    {
        List<EducationStat> list = educationStatService.selectEducationStatList(educationStat);
        ExcelUtil<EducationStat> util = new ExcelUtil<EducationStat>(EducationStat.class);
        util.exportExcel(response, list, "研究机构-教育部统计数据");
    }

    /**
     * 获取研究机构-教育部统计详细信息
     */
    @PreAuthorize("@ss.hasPermi('research:educationStat:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(educationStatService.selectEducationStatById(id));
    }

    /**
     * 新增研究机构-教育部统计
     */
    @PreAuthorize("@ss.hasPermi('research:educationStat:add')")
    @Log(title = "研究机构-教育部统计", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EducationStat educationStat)
    {
        EducationStat temp = new EducationStat();
        temp.setStatYear(educationStat.getStatYear());
        temp.setResearchUnitId(educationStat.getResearchUnitId());
        List<EducationStat> list = educationStatService.selectEducationStatList(temp);
        if (list.size() > 0) {
            return AjaxResult.error("已经做了当前年度的统计，请重新选择统计年度！");
        }

        return toAjax(educationStatService.insertEducationStat(educationStat));
    }

    /**
     * 修改研究机构-教育部统计
     */
    @PreAuthorize("@ss.hasPermi('research:educationStat:edit')")
    @Log(title = "研究机构-教育部统计", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EducationStat educationStat)
    {
        EducationStat temp = new EducationStat();
        temp.setStatYear(educationStat.getStatYear());
        temp.setResearchUnitId(educationStat.getResearchUnitId());
        List<EducationStat> list = educationStatService.selectEducationStatList(temp);
        if (list.size() > 1 || (list.size() == 1 && !list.get(0).getId().equals(educationStat.getId()))) {
            return AjaxResult.error("已经做了当前年度的统计，请重新选择统计年度！");
        }

        return toAjax(educationStatService.updateEducationStat(educationStat));
    }

    /**
     * 删除研究机构-教育部统计
     */
    @PreAuthorize("@ss.hasPermi('research:educationStat:remove')")
    @Log(title = "研究机构-教育部统计", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(educationStatService.deleteEducationStatByIds(ids));
    }

    @PreAuthorize("@ss.hasPermi('research:educationStat:list')")
    @GetMapping("/isExistStatYear")
    public AjaxResult isExistStatYear(EducationStat educationStat)
    {
        List<EducationStat> list = educationStatService.selectEducationStatList(educationStat);
        if (StringUtils.isEmpty(educationStat.getId())) {
            if (list.size() > 0) {
                return AjaxResult.success(true);
            }
        } else {
            if (list.size() > 1 || (list.size() == 1 && !list.get(0).getId().equals(educationStat.getId()))) {
                return AjaxResult.success(true);
            }
        }
        return AjaxResult.success(false);
    }
}
